package com.naiterui.ehp.bs.doctor.controller;

import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.bo.HeaderParams;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.support.security.HeaderConstant;
import com.naiterui.ehp.bs.doctor.exception.ExceptionCodes;
import com.naiterui.ehp.bs.doctor.prodocl.ReceiveInfo;
import com.naiterui.ehp.bs.doctor.service.IDrBatchMessageService;
import com.naiterui.ehp.bs.doctor.vo.BaseDrSendInfoVo;
import com.naiterui.ehp.bs.doctor.vo.BatchMessageVo;
import com.naiterui.ehp.bs.doctor.vo.PatientGroupVo;
import com.naiterui.ehp.bs.doctor.vo.SendInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.BATCH_MESSAGE})
@RestController
@AllArgsConstructor
@RequestMapping("ad/batch/message")
public class AdDrBatchMessageController {

    private final IDrBatchMessageService drBatchMessageService;

    /**
     * 获取群发次数
     *
     * @return
     */
    @PostMapping("count/num")
    @ApiOperation(value = "获取群发次数", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId", value = "医生ID", paramType = "form", dataType = "long")
    })
    public SendInfoVo getSendInfo(Long doctorId) throws BusinessException {
        log.info("DrMessageController methon getSendInfo doctorId：{}", doctorId);
        if (doctorId == null) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        return this.drBatchMessageService.getSendInfo(doctorId);
    }

    /**
     * 确认发送
     *
     * @return
     */
    @PostMapping("send")
    @ApiOperation(value = "获取群发次数", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "doctorId", value = "医生ID", paramType = "form", dataType = "long")
    })
    public BaseDrSendInfoVo send(Long doctorId, String message, Integer receiveType, String patientIds, String groupIds,
                                 HttpServletRequest request) throws BusinessException {
        log.info("DrMessageController methon send doctorId:{},message:{},receiveType:{},patientIds:{}," + "groupIds:{}",
                 doctorId, message, receiveType, patientIds, groupIds);
        if (doctorId == null || receiveType == null) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        if (receiveType == ReceiveInfo.DEFINED && (StringUtils.isBlank(patientIds) || patientIds.split(",").length == 0)) {

            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        if (receiveType == ReceiveInfo.GROUP && (StringUtils.isBlank(groupIds) || groupIds.split(",").length == 0)) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        if (receiveType == ReceiveInfo.GROUP_REVERSE && (StringUtils.isBlank(groupIds) || groupIds.split(",").length == 0)) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }

        HeaderParams headerParams = this.buildHeaderParams(request);
        ReceiveInfo receiveInfo = this.buildReceiveInfo(receiveType, patientIds, groupIds);
        this.drBatchMessageService.addBatchMessage(doctorId, message, headerParams, receiveInfo);
        return this.drBatchMessageService.getCountNum(doctorId);
    }

    /**
     * 群发历史
     *
     * @return
     */
    @PostMapping("list")
    @ApiOperation(value = "群发历史", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public PageVO<BatchMessageVo> list(Long doctorId, PageParamVO pageVO) throws BusinessException {
        log.info("DrMessageController methon list doctorId：{},page：{},num：{}", doctorId, pageVO.getPage(), pageVO.getNum());
        if (doctorId == null) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        return this.drBatchMessageService.getList(doctorId, pageVO.getPage(), pageVO.getNum());
    }

    /**
     * 消息删除
     */
    @PostMapping("remove")
    @ApiOperation(value = "消息删除", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public void remove(Long doctorId, String id) throws BusinessException {
        log.info("DrMessageController methon remove doctorId：{},id：{}", doctorId, id);
        if (doctorId == null || id == null) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        this.drBatchMessageService.delete(doctorId, id);
    }

    /**
     * 清除3月以前的群发记录
     */
    @PostMapping("removeTask")
    @ApiOperation(value = "清除3月以前的群发记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public void removeTask() {
        log.info("DrMessageController methon removeTask");
        this.drBatchMessageService.deleteTask();
    }

    /**
     * 获取分组列表
     */
    @ApiOperation(value = "获取分组列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("group/list")
    public ResponseVO<List<PatientGroupVo>> groupList(Long doctorId) {

        log.info("DrMessageController methon groupList doctorId：{}", doctorId);
        List<PatientGroupVo> vo = this.drBatchMessageService.findPatientGroupByDoctorId(doctorId);

        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, vo);
    }

    /**
     * 构建header信息
     */
    public HeaderParams buildHeaderParams(HttpServletRequest request) {
        HeaderParams headerParams = new HeaderParams();
        String platForm = request.getHeader(HeaderConstant.HEADER_PLATFORM);
        String origin = request.getHeader(HeaderConstant.HEADER_ORIGIN);
        String isNative = request.getHeader(HeaderConstant.HEADER_VERSION);
        String version = request.getHeader(HeaderConstant.HEADER_NATIVE);
        headerParams.setPlatForm(platForm);
        headerParams.setOrigin(origin);
        headerParams.setIsNative(isNative);
        headerParams.setVersion(version);
        return headerParams;
    }

    /**
     * 构建接收人信息
     */
    public ReceiveInfo buildReceiveInfo(Integer receiveType, String patientIds, String groupIds) {
        ReceiveInfo receiveInfo = new ReceiveInfo();
        receiveInfo.setGroupIds(groupIds);
        receiveInfo.setPatientIds(patientIds);
        receiveInfo.setReceiveType(receiveType);
        return receiveInfo;
    }

}

